(function ($) {
	/**
	 * 校验器辅助工具
	 */
	$.jqFormatUtils = $.jqFormatUtils || {};
	$.extend($.jqFormatUtils,{
		/**
		 * 去除前空格
		 */
		trimBegin:function(element){
			if (/(^[\s]+)/g.test(element.val())) { 
				element.val((element.val()).replace(/(^[\s]*)/g,''));
			}
		},
		/**
		 * 去除后空格
		 */
		trimEnd:function(element){
			if (/([\s]+$)/g.test(element.val())) { 
				element.val((element.val()).replace(/([\s]*$)/g,''));
			}
		},
		/**
		 * 去除前后空格
		 */
		trimBoth:function(element){
			if (/(^[\s]+)|([\s]+$)/g.test(element.val())) { 
				element.val((element.val()).replace(/(^[\s]*)|([\s]*$)/g,''));
			}
		},
		/**
		 * 去除所有空格
		 */
		trimAll:function(element){
			if(/([\s]+)/g.test(element.val())) {
				var result = (element.val()).replace(/(^[\s]*)|([\s]*$)/g,'');
				result = result.replace(/\s/g,"");
				element.val(result);
			}
		},
		/**
		 * 转成大写
		 */
		toUpperCase:function(element){
			var reg = /^[A-Z]+$/;
			if(!reg.test(element.val())) {
				var result = (element.val()).toUpperCase();
				element.val(result);
			}
		},
		/**
		 * 转成小写
		 */
		toLowerCase:function(element){
			var reg = /^[a-z]+$/;
			if(!reg.test(element.val())) {
				var result = (element.val()).toLowerCase();
				element.val(result);
			}
		},
		/**
		 * 格式化货币
		 */
		formatCurrency : function(element){
			var money = element.val();
			money = money.replace(/,/g, "");
			if (/[^0-9\.]/.test(money))
				return '';
			money = money.replace(/^(\d*)$/, "$1&");
			var re = /(\d)(\d{3}\.)/;
			if (/^(\d*)&$/.test(money))
				re = /(\d)(\d{3}&)/;
				while (re.test(money)) {
					money = money.replace(re, "$1,$2");
					re = /(\d)(\d{3},)/;
				}
				money = money.replace("&", "");
				element.val(money);
		},
		/**
		 * 反格式化货币
		 */
		reformatCurrency : function(element){
			var money = element.val();
			money = money.substring(0, money.length - 2)
			money = money.replace('。', '.').replace('￥', '').replace(
								'，', '').replace(/,/g, '').replace('.', '');
			element.val(money);
		},
		/**
		 * 数字类型格式化,仅用于onkeypress
		 * event : 事件对象
		 * obj : 控件dom对象
		 * positive	: 是否为正数
		 * intPart : 小数前整数部分位数
		 * decPart : 小数部分位数
		 */
		number : function(event, obj, positive, intPart, decPart) {
			var originalValue = obj.value.replace(/,/g, '');
			if (event.keyCode) {
				if(event.shiftKey === true){
					return false;
				}
				if (event.keyCode != 9 && event.keyCode != 11 && event.keyCode != 35
						&& event.keyCode != 36 // END/HOME
						&& event.keyCode != 37 && event.keyCode != 38 // 方向键
						&& event.keyCode != 39 && event.keyCode != 40 // 方向键
						&& event.keyCode != 45 && event.keyCode != 46
						&& event.keyCode != 127 && event.keyCode != 8
						&& (event.keyCode < 48 || event.keyCode > 57)) {
					// ie6,7,8,opera,chrome管用
					return false;
				} else if (event.keyCode == 9 || event.keyCode == 11
						|| event.keyCode == 35 || event.keyCode == 36
						|| event.keyCode == 37 || event.keyCode == 38
						|| event.keyCode == 39 || event.keyCode == 40
						|| event.keyCode == 127 || event.keyCode == 8) {// 去掉46,避免输入多个小数点
					// Do Nothing:功能键无需进行详细判断，直接可用。
				} else {
					var selectStart = $.jqFormatUtils.getNumberTextSelectionStart(obj);
					var selectEnd = $.jqFormatUtils.getNumberTextSelectionEnd(selectStart, obj);
					originalValue = originalValue.substring(0, selectStart)
							+ originalValue.substring(selectEnd, originalValue.length);
					if (originalValue != null && originalValue != undefined
							&& originalValue.length != 0) {
						// 输入减号
						if (event.keyCode == 45) {
							if (positive) {
								return false;
							}
							if (originalValue.indexOf('-') == 0) {
								if (selectStart != 0 || selectEnd == 0) {
									return false;
								}
							} else {
								if (selectStart != 0) {
									return false;
								}
							}
							return true;
						} else if (event.keyCode == 46) {// 输入小数点
							if (originalValue == '-'
									|| originalValue.indexOf('.') != -1
									|| selectEnd == originalValue.indexOf('-') + 1
									|| decPart == 0) {
								return false;
							}
							return true;
						} else if (originalValue.indexOf('.') == -1) {// 没有小数点，限制整数部分
							if (((originalValue.indexOf('-') != -1 && originalValue.length == intPart + 1) || (originalValue
									.indexOf('-') == -1 && originalValue.length == intPart))
									&& event.keyCode != 46) {
								return false;
							}
						} else {
							if (originalValue.indexOf('-') == -1) {
								var strIntPart = originalValue.substring(0,
										originalValue.indexOf('.'));
							} else {
								var strIntPart = originalValue.substring(1,
										originalValue.indexOf('.'));
							}

							if (selectStart <= originalValue.indexOf('.')) {
								if (strIntPart.length == intPart) {
									return false;
								}
							} else {
								var strDecPart = originalValue.substring(originalValue
										.indexOf('.') + 1, originalValue.length);
								if (strDecPart.length == decPart) {
									return false;
								}
							}
						}
					} else if (event.keyCode == 46 || (event.keyCode == 45 && positive)) {
						return false;
					}
				}
			} else {
				if(event.shiftKey === true){
					event.preventDefault();
				}
				if (event.charCode
						&& (event.charCode != 9 && event.charCode != 11
								&& event.charCode != 35 && event.charCode != 36 // END/HOME
								&& event.charCode != 37 && event.charCode != 38 // 方向键
								&& event.charCode != 39 && event.charCode != 40 // 方向键
								&& event.charCode != 45 && event.charCode != 46
								&& event.charCode != 127 && event.charCode != 8 && (event.charCode < 48 || event.charCode > 57))) {
					// firefox管用
					event.preventDefault();
				} else if (event.charCode == 0 || event.charCode == 9
						|| event.charCode == 11 || event.charCode == 35
						|| event.charCode == 36 || event.charCode == 37
						|| event.charCode == 38 || event.charCode == 39
						|| event.charCode == 40 || event.charCode == 127
						|| event.charCode == 8) {
					// Do Nothing:功能键无需进行详细判断，直接可用。
				} else {
					originalValue = originalValue.substring(0, obj.selectionStart)
							+ originalValue.substring(obj.selectionEnd,
									originalValue.length);
					if (originalValue != null && originalValue != undefined
							&& originalValue.length != 0) {
						// 输入减号
						if (event.charCode == 45) {
							if (positive) {
								event.preventDefault();
							}
							if (originalValue.indexOf('-') == 0) {
								if (obj.selectionStart != 0 || obj.selectionEnd == 0) {
									event.preventDefault();
								}
							} else {
								if (obj.selectionStart != 0) {
									event.preventDefault();
								}
							}
							return true;
						} else if (event.charCode == 46) { // 输入小数点
							if (originalValue == '-'
									|| originalValue.indexOf('.') != -1
									|| obj.selectionEnd == originalValue.indexOf('-') + 1
									|| decPart == 0) {
								event.preventDefault();
							}
							return true;
						} else if (originalValue.indexOf('.') == -1) {// 没有小数点，限制整数部分
							if (((originalValue.indexOf('-') != -1 && originalValue.length == intPart + 1) || (originalValue
									.indexOf('-') == -1 && originalValue.length == intPart))
									&& event.charCode != 46) {
								event.preventDefault();
							}
						} else {
							if (originalValue.indexOf('-') == -1) {
								var strIntPart = originalValue.substring(0,
										originalValue.indexOf('.'));
							} else {
								var strIntPart = originalValue.substring(1,
										originalValue.indexOf('.'));
							}

							if (obj.selectionStart <= originalValue.indexOf('.')) {
								if (strIntPart.length == intPart) {
									event.preventDefault();
								}
							} else {
								var strDecPart = originalValue.substring(originalValue
										.indexOf('.') + 1, originalValue.length);
								if (strDecPart.length == decPart) {
									event.preventDefault();
								}
							}
						}
					} else if (event.charCode == 46
							|| (event.charCode == 45 && positive)) {
						event.preventDefault();
					}
				}
			}
		},
		/**
		 * $.jqFormatUtils私有方法
		 */
		getNumberTextSelectionStart : function(obj) {
			if (obj.document) {
				var curRange = obj.document.selection.createRange();// 取得当前光标所在位置的TextRange
				var tbRange = obj.createTextRange();// 整个输入框的TextRange
				tbRange.collapse(true);// 将光标移到头部(TextRange的头与尾的位置相等并等于头部值)
				tbRange.select();// 执行一次选择,改变document.selection的内容，collapse同时移动头和尾，实际上返回的是空的TextRange。
				var headRange = obj.document.selection.createRange();// 取得头部的TextRange
				headRange.setEndPoint("EndtoEnd", curRange);// 尾部与开始光标的尾部相同。
				var pos = headRange.text.length;
				curRange.select();// 恢复光标位置
				return pos - curRange.text.length;
			}
		},
		/**
		 * $.jqFormatUtils私有方法
		 */
		getNumberTextSelectionEnd : function(posStart, obj) {
			if (obj.document) {
				var curRange = obj.document.selection.createRange();// 取得当前光标所在位置的TextRange
				return posStart + curRange.text.length;
			}
		}
	});
})(jQuery);
